<template>
  <div>
    <input ref='PDAScan' v-model='PDACode' @focus.prevent='blurscan' @change='inputscan' autofocus
      style='position: fixed; top: -0.1rem; opacity: 0; width: 0.1rem' />
  </div>
</template>

<script>
// PDA扫码页面
export default {
  name: 'Analysis',
  props: {
    saoparams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  components: {},
  data() {
    return {
      data: [{}],
      show: true,
      PDACode: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initFocus()
    })
    document.onkeydown = (e) => {
      if (e.keyCode == '13') {
        console.log(13)
        this.$refs.PDAScan.focus()
      }
    }
  },
  unmounted() {
    this.data = []
  },
  methods: {
    initFocus() {
      this.$refs.PDAScan.focus()
    },
    // PDA扫码输入框聚焦时不调用手机键盘
    blurscan() {
      this.$refs.PDAScan.setAttribute('readonly', 'readonly')
      setTimeout(() => {
        this.$refs.PDAScan.removeAttribute('readonly')
      }, 200)
    },
    // PDA扫码
    inputscan(v) {
      let str = v.target.value
      let cleanedResult = str.replace(/\s/g, '') // 清理不可见字符
      let parsedResult
      try {
        if (cleanedResult.includes("。")) {
          this.$antdMessage.error('请将输入法切换为英文再使用扫码枪操作')
          return
        }
        if (cleanedResult === '.') {
          parsedResult = cleanedResult
        }
        else {
          parsedResult = JSON.parse(cleanedResult)
        }
        if (parsedResult === 0 || parsedResult) {
          this.$emit('changeScan', parsedResult, 'scan')
          this.PDACode = ''
        } else {
          // 扫描结果无效
          this.$antdMessage.error('请扫正确的二维码')
          this.PDACode = ''
        }
      } catch (error) {
        this.$antdMessage.error('请扫正确的二维码')
        this.PDACode = ''
      }
    }
  }
}
</script>
<style lang='less' scoped>
.topbtn {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 0.05rem 0.06rem;
  border-bottom: 1px solid #e9e9e9;

  .title {
    width: 80%;
    text-align: center;
  }

  .oksum {
    color: #1890ff;
  }
}

.data {
  padding: 0.1rem;
}

.table {
  overflow-x: hidden;
  overflow-y: auto;
  text-align: center;

  .tr {
    width: 100%;

    .wit {
      width: 10%;
    }

    .th {
      text-align: center;
    }

    .td {
      text-align: center;
      word-wrap: break-word;
    }
  }
}

.tdbtn {
  color: #ff0000 !important;
}
</style>
